package com.gzjh.basic.common;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.alibaba.druid.util.StringUtils;

public class BaseDao<T, PK extends Serializable> extends HibernateDaoSupport implements IBaseDao<T, PK> {

	@Override
	public Serializable save(T t) {
		return getHibernateTemplate().save(t);
	}

	@Override
	public void update(T t) {
		getHibernateTemplate().update(t);
	}

	@Override
	public void delete(T t) {
		getHibernateTemplate().delete(t);
	}

	@Override
	public T get(Class<T> entityClass, PK pk) {
		return getHibernateTemplate().get(entityClass, pk);
	}

	@Override
	public List<T> getAll(Class<T> entityClass) {
		return null;
	}

	@Override
	public T load(Class<T> entityClass, PK pk) {
		return getHibernateTemplate().load(entityClass, pk);
	}

	@Override
	public List<T> loadAll(Class<T> entityClass) {
		return getHibernateTemplate().loadAll(entityClass);
	}

	@Override
	public void saveOrUpdate(T t) {
		getHibernateTemplate().saveOrUpdate(t);
	}

	@Override
	public void deleteAll(Collection<?> entities) {
		getHibernateTemplate().deleteAll(entities);
	}

	@Override
	public List<?> findByCriteria(DetachedCriteria criteria) {
		return getHibernateTemplate().findByCriteria(criteria);
	}

	@Override
	public List<?> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) {
		return getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults);
	}

	@Override
	public List<T> createNativeQuery(String sqlString, Class<T> resultClass) {
		if(StringUtils.isEmpty(sqlString)) {
			throw new NullPointerException("查询的sql语句不能为空");
		}
		NativeQuery<T> nativeQuery = currentSession().createNativeQuery(sqlString, resultClass);
		return nativeQuery.getResultList();
	}

	@Override
	public List<?> createNativeQuery(String sqlString) {
		if(StringUtils.isEmpty(sqlString)) {
			throw new NullPointerException("查询的sql语句不能为空");
		}
		NativeQuery<?> nativeQuery = currentSession().createNativeQuery(sqlString);
		return nativeQuery.getResultList();
	}
	
	@Override
	public List<?> createNativeQuery(String sqlString, Object[] params) {
		if(StringUtils.isEmpty(sqlString)) {
			throw new NullPointerException("查询的sql语句不能为空");
		}
		NativeQuery<?> nativeQuery = currentSession().createNativeQuery(sqlString);
		int position = 0;
		if(params != null) {
			for (Object param : params) {
				nativeQuery.setParameter(position, param);
			}
		}
		return nativeQuery.getResultList();
	}

	@Override
	public List<T> createNativeQuery(String sqlString, Class<T> resultClass, int firstResult, int maxResult) {
		if(StringUtils.isEmpty(sqlString)) {
			throw new NullPointerException("查询的sql语句不能为空");
		}
		NativeQuery<T> nativeQuery = currentSession().createNativeQuery(sqlString, resultClass);
		nativeQuery.setFirstResult(firstResult);
		nativeQuery.setMaxResults(maxResult);
		return nativeQuery.getResultList();
	}
	
	@Override
	public List<T> createNativeQuery(String sqlString, Object[] params, Class<T> resultClass, int firstResult, int maxResult) {
		if(StringUtils.isEmpty(sqlString)) {
			throw new NullPointerException("查询的sql语句不能为空");
		}
		NativeQuery<T> nativeQuery = currentSession().createNativeQuery(sqlString, resultClass);
		nativeQuery.setFirstResult(firstResult);
		nativeQuery.setMaxResults(maxResult);
		int position = 0;
		if(params != null) {
			for (Object param : params) {
				nativeQuery.setParameter(position, param);
			}
		}
		return nativeQuery.getResultList();
	}

	@Override
	public List<T> findByHql(String hqlString, Object[] params, Class<T> resultClass) {
		if(StringUtils.isEmpty(hqlString)) {
			throw new NullPointerException("查询的hql语句不能为空");
		}
		Query<T> query = currentSession().createQuery(hqlString, resultClass);
		int position = 0;
		if(params != null) {
			for (Object param : params) {
				query.setParameter(position, param);
			}
		}
		return query.getResultList();
	}

	@Override
	public List<T> findByHql(String hqlString, Object[] params, Class<T> resultClass, int firstResult, int maxResult) {
		if(StringUtils.isEmpty(hqlString)) {
			throw new NullPointerException("查询的hql语句不能为空");
		}
		
		Query<T> query = currentSession().createQuery(hqlString, resultClass);
		query.setFirstResult(firstResult);
		query.setMaxResults(maxResult);
		
		int position = 0;
		if(params != null) {
			for (Object param : params) {
				query.setParameter(position, param);
			}
		}
		
		return query.getResultList();
	}

	@Override
	public List<?> findByHql(String hqlString, Object[] params) {
		if(StringUtils.isEmpty(hqlString)) {
			throw new NullPointerException("查询的hql语句不能为空");
		}
		
		Query<?> query = currentSession().createQuery(hqlString);
		
		int position = 0;
		if(params != null) {
			for (Object param : params) {
				query.setParameter(position, param);
			}
		}
		
		return query.getResultList();
	}

	@Override
	public List<?> findByHql(String hqlString, Object[] params, int firstResult, int maxResult) {
		if(StringUtils.isEmpty(hqlString)) {
			throw new NullPointerException("查询的hql语句不能为空");
		}
		
		Query<?> query = currentSession().createQuery(hqlString);
		query.setFirstResult(firstResult);
		query.setMaxResults(maxResult);
		
		int position = 0;
		if(params != null) {
			for (Object param : params) {
				query.setParameter(position, param);
			}
		}
		
		return query.getResultList();
	}
	
}
